﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace DataAccessor.Tools
{
    public delegate void StatementCollectionChangedEventHandler();
    public partial class FormStatement : Form
    {
        private XmlDocument _configDoc;
        private HolisticDatabaseConfig _config;
        private TSQL _tsql;

        #region events
        public event StatementCollectionChangedEventHandler StatementCollectionChanged;
        private void OnStatementCollectionChanged()
        {
            if (StatementCollectionChanged != null)
            {
                StatementCollectionChanged();
            }
        }
        public event DatabaseChangedEventHandler DatabaseSettingChanged;
        private void OnDatabaseSettingChanged(UCDatabase sender)
        {
            if (DatabaseSettingChanged != null)
            {
                DatabaseSettingChanged(sender);
            }
        }
        #endregion

        public FormStatement(HolisticDatabaseConfig config, TSQL tsql)
        {
            InitializeComponent();
            this._config = config;
            this._tsql = tsql;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadStatement();
        }

        private void pictureCreate_Click(object sender, EventArgs e)
        {
            var splitter = BuildSplitter();
            var settingForm = BuildStatementControl();
            settingForm.Tag = splitter;
            List<Control> range = new List<Control> { splitter, settingForm };
            panelSettingsContainer.Controls.AddRange(range.ToArray());

            var statement = settingForm.Statement;
            List<TSQLStatement> statementList = null;
            if (_tsql.Statement == null)
            {
                statementList = new List<TSQLStatement>();
            }
            else
            {
                statementList = _tsql.Statement.ToList();
            }
            statementList.Add(statement);
            _tsql.Statement = statementList.ToArray();
            OnStatementCollectionChanged();
        }

        private void LoadStatement()
        {
            if (_tsql.Statement == null)
            {
                return;
            }
            List<Control> range = new List<Control>();
            foreach (var statement in _tsql.Statement)
            {
                var splitter = BuildSplitter();
                var settingForm = BuildStatementControl(statement);
                settingForm.MiniMode = true;
                settingForm.Tag = splitter;

                range.Add(splitter);
                range.Add(settingForm);
            }
            panelSettingsContainer.Controls.AddRange(range.ToArray());
        }

        private UCStatement BuildStatementControl(TSQLStatement statement = null)
        {
            UCStatement settingForm = new UCStatement()
            {
                Dock = DockStyle.Top,
                Statement = statement,
                ContextMenuStrip = contextMenu
            };
            settingForm.StatementChanged += new StatementChangedEventHandler(settingForm_StatementChanged);
            settingForm.Show();
            return settingForm;
        }

        void settingForm_StatementChanged(UCStatement sender)
        {
            var a = sender.Statement;
        }

        private Control BuildSplitter()
        {
            Panel splitter = new Panel()
            {
                Dock = DockStyle.Top,
                Height = 5
            };
            return splitter;
        }

        private void cmiDel_Click(object sender, EventArgs e)
        {
            var settingForm = contextMenu.SourceControl.Parent as UCStatement;
            var splitter = settingForm.Tag as Panel;

            var satatementList = _tsql.Statement.ToList();
            satatementList.Remove(settingForm.Statement);

            _tsql.Statement = satatementList.ToArray();
            OnStatementCollectionChanged();

            settingForm.Parent.Controls.Remove(splitter);
            settingForm.Parent.Controls.Remove(settingForm);

            splitter.Dispose();
            settingForm.Dispose();
        }
    }
}
